package com.newtype.meeting.client.widget.backend;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.MeetingCategory;

/**
 * 新增/维护主类别窗口
 * @author 汪恭泽
 * Jun 13, 2011 11:30:29 AM
 */
public class MainCategoryWindow extends Window {
	private BeanModel categoryBeanModel;

	public MainCategoryWindow(MeetingCategory category, boolean flag) {
		setHeading("[ 新增/维护 类别 ]");
		setWidth("350");
		setAutoHeight(true);
		setModal(true);
		setButtonAlign(HorizontalAlignment.CENTER);

		FormPanel formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setBorders(false);

		TextField<String> mainCategoryName = new TextField<String>();
		mainCategoryName.setFieldLabel("主类别名称");
		mainCategoryName.setName("categoryName");
		formPanel.add(mainCategoryName);

		categoryBeanModel = BeanModelLookup.get().getFactory(
				MeetingCategory.class).createModel(category);

		FormBinding formBinding = new FormBinding(formPanel, true);
		formBinding.bind(categoryBeanModel);

		addButton(getSaveButton());
		if (category.getId() != null) {
			addButton(getDeleteButton(flag));
		}
		add(formPanel);
	}

	private Button getSaveButton() {
		Button button = new Button("送出");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				MeetingCategory category = categoryBeanModel.getBean();
				Dispatcher.forwardEvent(AppEvents.SaveorUpdateMainCategory,
						category);
				hide();
			}
		});
		return button;
	}

	private Button getDeleteButton(final boolean flag) {
		Button button = new Button("删除");
		final MeetingCategory category = categoryBeanModel.getBean();
		final Listener<MessageBoxEvent> listener = new Listener<MessageBoxEvent>() {
			public void handleEvent(MessageBoxEvent ce) {
				Button btn = ce.getButtonClicked();
				if (btn.getText().equals("Yes")) {
					Dispatcher.forwardEvent(AppEvents.DeleteMainCategory,
							category);
					hide();
				}
			}
		};
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (flag) {
					MessageBox.alert("提示", "该资料使用中，不允许删除", null);
				} else {
					MessageBox.confirm("提示", "你确定要删除这笔数据吗? 删除的数据无法还原 !",
							listener);
				}
			}
		});
		return button;
	}
}
